package cn.ava.ymcc.web.controller;

import cn.ava.ymcc.domain.CourseChapter;
import cn.ava.ymcc.dto.CourseSaveDto;
import cn.ava.ymcc.service.ICourseService;
import cn.ava.ymcc.domain.Course;
import cn.ava.ymcc.query.CourseQuery;
import cn.ava.ymcc.result.JSONResult;
import cn.ava.ymcc.result.PageList;
import cn.ava.ymcc.vo.CourseInfoVo;
import cn.ava.ymcc.vo.Courseinfo2OrderinfoVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
     * 首页展示推荐
     * @param courseId
     * @return
     */
    @RequestMapping(value="/recommendCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult recommendCourse(@PathVariable @NotNull(message = "参数异常!") Long courseId){
        courseService.recommendCourse(courseId);
        return JSONResult.success();
    }

    /**
     * 首页展示最热门五条数据
     * @param
     * @return
     */
    @RequestMapping(value="/hotCourse",method= RequestMethod.POST)
    public JSONResult hotCourse(){
        List<Course> courses = courseService.hotCourse();
        return JSONResult.success(courses);
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody @Valid CourseSaveDto courseDto){
            courseService.save(courseDto);
        return JSONResult.success();
    }

    /**
     * 课程发布上架
     * @param courseId
     * @return
     */
    @PreAuthorize("hasAuthority('course:online')")
    @RequestMapping(value="/onLineCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable Long courseId){
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }

    /**
     * 课程下线
     * @param courseId
     * @return
     */
    @RequestMapping(value="/offLineCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable Long courseId){
        courseService.offLineCourse(courseId);
        return JSONResult.success();
    }

    /**
     * 首页商品分类面包屑展示
     * @param courseTypeId
     * @return
     */
    @RequestMapping(value="/courseType/crumbs/{courseTypeId}",method= RequestMethod.GET)
    public JSONResult crumbs(@PathVariable Long courseTypeId){
        return JSONResult.success(courseService.crumbs(courseTypeId));
    }

    /**
     * 获取课程详情数据,渲染到课程详情页
     * @param courseId
     * @return
     */
    @RequestMapping(value="/detail/data/{courseId}",method= RequestMethod.GET)
    public JSONResult CourseDetailData(@PathVariable @NotNull(message = "参数异常!") Long courseId){
        CourseInfoVo courseInfoVos = courseService.CourseDetailData(courseId);
        return JSONResult.success(courseInfoVos);
    }

    /**
     * 获取课程信息数据渲染下单页面
     * @param courseIds
     * @return
     */
    @RequestMapping(value="/info/{courseIds}",method= RequestMethod.GET)
    public JSONResult info(@PathVariable @NotEmpty(message = "参数异常!") String courseIds){
        Courseinfo2OrderinfoVo Courseinfo2OrderinfoVo = courseService.info(courseIds);
        return JSONResult.success(Courseinfo2OrderinfoVo);
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @PreAuthorize("hasAuthority('course:list')")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = courseService.selectPage(query);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
